using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class UpgradeMemberComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game _game;
        private SpriteBatch _spriteBatch;
        private SpriteFont _textFont;
        private Vector2 touchPosition, pretouchPosition;
        private List<int> _listPrice = new List<int>() { 1000, 1800, 4000, 6000, 7500, 12500, 17000 };
        private List<Button> _listAHero;
        private List<Button> _listPriceButton;

        //Initilation
        public UpgradeMemberComponent(Game game, List<Texture2D> listIconAHero, SpriteFont textFont, SpriteFont minitextFont, SpriteBatch spriteBatch)
            : base(game)
        {
            _game = game;
            _textFont = textFont;
            _spriteBatch = spriteBatch;

            //Init Button
            _listAHero = new List<Button>();
            _listPriceButton = new List<Button>();
            _listAHero.Add(new Button(listIconAHero[0], new Vector2(50, 190)));     _listPriceButton.Add(new Button("$" + _listPrice[0], minitextFont, new Vector2(65, 300), Color.Yellow) { HaveAnimation = false });
            _listAHero.Add(new Button(listIconAHero[1], new Vector2(170, 190)));    _listPriceButton.Add(new Button("$" + _listPrice[1], minitextFont, new Vector2(185, 300), Color.Yellow) { HaveAnimation = false });
            _listAHero.Add(new Button(listIconAHero[2], new Vector2(290, 190)));    _listPriceButton.Add(new Button("$" + _listPrice[2], minitextFont, new Vector2(305, 300), Color.Yellow) { HaveAnimation = false });
            _listAHero.Add(new Button(listIconAHero[3], new Vector2(410, 190)));    _listPriceButton.Add(new Button("$" + _listPrice[3], minitextFont, new Vector2(425, 300), Color.Yellow) { HaveAnimation = false });
            _listAHero.Add(new Button(listIconAHero[4], new Vector2(170, 330)));    _listPriceButton.Add(new Button("$" + _listPrice[4], minitextFont, new Vector2(180, 440), Color.Yellow) { HaveAnimation = false });
            _listAHero.Add(new Button(listIconAHero[5], new Vector2(290, 330)));    _listPriceButton.Add(new Button("$" + _listPrice[5], minitextFont, new Vector2(300, 440), Color.Yellow) { HaveAnimation = false });
            _listAHero.Add(new Button(listIconAHero[6], new Vector2(410, 330)));    _listPriceButton.Add(new Button("$" + _listPrice[6], minitextFont, new Vector2(420, 440), Color.Yellow) { HaveAnimation = false });
            for (int i = 0; i < _listAHero.Count; i++)
                _listAHero[i].Selected += new Button.IsSelected(UpgradeMemberSelected);
        }

        void UpgradeMemberSelected(int input)
        {
            if (_listAHero[input].ColorTexture != Color.Gray && ManagerGame.luffy.Balance >= _listPrice[input])
            {
                ManagerGame.luffy.ListAHeroHave.Add(input);
                ManagerGame.luffy.Balance -= _listPrice[input];
            }
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < 6; i++)
            {
                _listAHero[i].ColorTexture = Color.White;
                _listPriceButton[i].Text = "$" + _listPrice[i].ToString();
            }
            for (int i = 0; i < ManagerGame.luffy.ListAHeroHave.Count; i++)
            {
                _listAHero[ManagerGame.luffy.ListAHeroHave[i]].ColorTexture = Color.Gray;
                _listPriceButton[ManagerGame.luffy.ListAHeroHave[i]].Text = " ";
            }
            // TODO: Add your update code here    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            //Key input
            if (TouchPanel.IsGestureAvailable)
            {
                // Read in the Gesture input
                GestureSample gesture = TouchPanel.ReadGesture();
                // Process Gestures
                if (gesture.GestureType == GestureType.Tap)
                {
                    touchPosition = gesture.Position;
                    for (int i = 0; i < _listAHero.Count; i++)
                        _listAHero[i].ButtonIsTouched(touchPosition, i);
                }
            }

            
                base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            for (int i = 0; i < _listAHero.Count; i++)
            {
                _listAHero[i].Draw(_spriteBatch, gameTime);
                _listPriceButton[i].Draw(_spriteBatch, gameTime);
            }
        }
    }
}
